package com.example.helloandroid;

import java.util.HashMap;

import android.media.AudioManager;
import android.speech.tts.TextToSpeech;
import android.util.Log;

public class SpeakName extends Thread
{
	private volatile boolean m_bRun = true;
	private volatile String  m_NameToSpeak = null;
	private final int MY_SILENCE_DURATION = 2500;
	
	public SpeakName ( String NameToSpeak)
	{
		WriteText ( "In SpeakName. Name = " + NameToSpeak );
		m_NameToSpeak = NameToSpeak;
		m_bRun = true;
	}
	
	public void run ()
	{
		/* Mute the ring tone */
		AudioManager AudMgr = com.example.helloandroid.HelloAndroid.GetAudioMgrHdl();		
		TextToSpeech hTTS = com.example.helloandroid.HelloAndroid.GetTTSHandle();
		HashMap<String,String> myHash = com.example.helloandroid.HelloAndroid.GetAudioHashMap();		
		int iCount = 0;
		while ( m_bRun && (iCount<3))
		{
			AudMgr.setStreamMute(AudioManager.STREAM_RING, true);			
			WriteText ( "In Thread." );			
			Speak ( m_NameToSpeak, hTTS, myHash );
			while (hTTS.isSpeaking())
			{
				WriteText ( "Speaking ..." );							
				// Do nothing
				int i = 0;
				++i;
			}
			AudMgr.setStreamMute(AudioManager.STREAM_RING, false);			
//			hTTS.playSilence(MY_SILENCE_DURATION, TextToSpeech.QUEUE_FLUSH, myHash);
			try 
			{
				Thread.sleep(MY_SILENCE_DURATION);
			} catch (InterruptedException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			iCount++;
		}
		hTTS.stop();
	}
	
	private void Speak( String Name, TextToSpeech hTTS, HashMap<String,String> myHash )
	{
		String strToSpeak = Name + " Calling.";	
		hTTS.speak(strToSpeak, TextToSpeech.QUEUE_FLUSH, myHash);	
	}
	
	public void StopSpeaking ()
	{
		m_bRun = false;
	}
	
	private void WriteText ( String msg )
	{
		Log.i("SpeakName",  msg );
	}
	
}
